package io.aircraft.modules.wms.controller;

import io.aircraft.common.annotation.LogOperation;
import io.aircraft.modules.wms.dto.StockDTO;
import io.aircraft.common.page.PageData;
import io.aircraft.common.utils.Result;
import io.aircraft.common.validator.AssertUtils;
import io.aircraft.common.validator.ValidatorUtils;
import io.aircraft.common.validator.group.AddGroup;
import io.aircraft.common.validator.group.DefaultGroup;
import io.aircraft.common.validator.group.UpdateGroup;
import io.aircraft.modules.wms.dto.StockDetailDTO;
import io.aircraft.modules.wms.service.StockService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Map;


/**
 * 库存管理
 *
 * @author Cycle gengruyi01@163.com
 * @since 1.0.0 2022-10-13
 */
@RestController
@RequestMapping("wms/stock")
public class StockController {
    @Autowired
    private StockService stockService;

    @GetMapping("page")
    @RequiresPermissions("wms:stock:page")
    public Result<PageData<StockDTO>> page(@ApiIgnore @RequestParam Map<String, Object> params) {
        PageData<StockDTO> page = stockService.page(params);

        return new Result<PageData<StockDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @RequiresPermissions("wms:stock:info")
    public Result<StockDTO> get(@PathVariable("id") Long id) {
        StockDTO data = stockService.get(id);

        return new Result<StockDTO>().ok(data);
    }

    /**
     * 获取商品的可用库存
     */
    @GetMapping("usage")
    public Result<StockDetailDTO> getUsageStock(@RequestParam Map<String, Object> params) {
        final StockDetailDTO data = stockService.getStockDetailsBySpu(params);
        return new Result<StockDetailDTO>().ok(data);
    }

    @PostMapping
    @LogOperation("保存")
    @RequiresPermissions("wms:stock:save")
    public Result save(@RequestBody StockDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        stockService.save(dto);

        return new Result();
    }

    @PutMapping
    @LogOperation("修改")
    @RequiresPermissions("wms:stock:update")
    public Result update(@RequestBody StockDTO dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        stockService.update(dto);

        return new Result();
    }

    @DeleteMapping
    @LogOperation("删除")
    @RequiresPermissions("wms:stock:delete")
    public Result delete(@RequestBody Long[] ids) {
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        stockService.delete(ids);

        return new Result();
    }
}